﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Security;

public partial class Comment : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            bindData();
        }
    }
    public void bindData()
    {
        int MaDV = int.Parse(Session["MaDV"].ToString());
        string connStr = System.Configuration.ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
        SqlConnection conn = new SqlConnection(connStr);
        conn.Open();
        SqlCommand com = new SqlCommand();
        com.Connection = conn;
        com.CommandText = "sp_showComment";
        com.Parameters.AddWithValue("@MaDV", MaDV);
        com.CommandType = CommandType.StoredProcedure;
        SqlDataAdapter da = new SqlDataAdapter();
        da.SelectCommand = com;
        DataSet ds = new DataSet();
        da.Fill(ds);
        DataList1.DataSource = ds.Tables[0].DefaultView;
        DataList1.DataBind();
        CollectionPager1.BindToControl = DataList1;
        CollectionPager1.DataSource = ds.Tables[0].DefaultView;
        DataList1.DataSource = CollectionPager1.DataSourcePaged;
        DataList1.DataBind();
        conn.Close();
    }
    protected void ReportSpamButton_Click(object sender, EventArgs e)
    {

    }
    protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName.Equals("ReportSpam"))
        {
            if (HttpContext.Current.User.Identity.Name.Equals(""))
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Redit", "alert('Bạn phải đăng nhập mới có thể báo cáo bình luận này là spam');", true);
                return;
            }
            string connStr = System.Configuration.ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
            SqlConnection conn = new SqlConnection(connStr);
            conn.Open();
            SqlCommand com = new SqlCommand();
            com.Connection = conn;
            com.CommandText = "sp_increaseCountSpam";
            com.Parameters.AddWithValue("@MaComment", e.CommandArgument.ToString());
            com.Parameters.AddWithValue("@Username", HttpContext.Current.User.Identity.Name);
            com.CommandType = CommandType.StoredProcedure;
            try
            {
                int check = com.ExecuteNonQuery();
                conn.Close();
                bindData();
            }
            catch (Exception)
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Redit", "alert('Bạn đã báo cáo spam cho bình luận này');", true);
                conn.Close();
                bindData();
            }
        }
        
    }
    protected void PostCommentButton_Click(object sender, EventArgs e)
    {
        if (HttpContext.Current.User.Identity.Name.Equals(""))
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Redit", "alert('Bạn phải đăng nhập mới có thể bình luận');", true);
            return;
        }
        if (ContentCommentTextbox.Text.Length<3)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Redit", "alert('Bình luận phải trên 3 kí tự');", true);
            return;
        }
        int MaDV = int.Parse(Session["MaDV"].ToString());
        string connStr = System.Configuration.ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
        SqlConnection conn = new SqlConnection(connStr);
        conn.Open();
        SqlCommand com = new SqlCommand();
        com.Connection = conn;
        com.CommandText = "sp_insertComment";
        string user = HttpContext.Current.User.Identity.Name;
        com.Parameters.AddWithValue("@Username", HttpContext.Current.User.Identity.Name);
        com.Parameters.AddWithValue("@MaDV", MaDV);
        com.Parameters.AddWithValue("@ContentComment", ContentCommentTextbox.Text);
        com.CommandType = CommandType.StoredProcedure;
        try
        {
            int check = com.ExecuteNonQuery();
            conn.Close();
            bindData();
            ContentCommentTextbox.Text = "";
        }
        catch (Exception)
        {
            Response.Redirect("Error when post comment");
            conn.Close();
        }
    }
    protected void CancelCommentButton_Click(object sender, EventArgs e)
    {
        ContentCommentTextbox.Text = "";
    }
}